﻿using System;
using System.IO;

namespace Smart.CE.Log
{
    /// <summary>
    /// 
    /// </summary>
    public class TextWriterLogListener : LogListener
    {
        private TextWriter writer;

        /// <summary>
        /// 
        /// </summary>
        public TextWriter Writer
        {
            get
            {
                return this.writer;
            }
            set
            {
                this.writer = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        public TextWriterLogListener(string fileName) : this(new StreamWriter(fileName, true))
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        public TextWriterLogListener(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            this.writer = new StreamWriter(stream);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="writer"></param>
        public TextWriterLogListener(TextWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            this.writer = writer;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="disposing"></param>
        protected override void Dispose(bool disposing)
        {
            if( disposing )
            {
                this.Close();
            }
            base.Dispose( disposing );
        }

        /// <summary>
        /// 
        /// </summary>
        public override void Close()
        {
            if( this.writer != null )
            {
                this.writer.Close();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public override void Flush()
        {
            if( this.writer != null )
            {
                this.writer.Flush();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        public override void Write(string message)
        {
            if( this.writer != null )
            {
                if( NeedIndent )
                {
                    WriteIndent();
                }
                this.writer.Write( message );
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        public override void WriteLine(string message)
        {
            if( this.writer != null )
            {
                if( NeedIndent )
                {
                    WriteIndent();
                }
                this.writer.WriteLine( message );

                NeedIndent = true;
            }
        }
    }
}
